package cn.liweixi.dictionary.controller;

import cn.liweixi.basic.utils.PageList;
import cn.liweixi.basic.utils.Result;
import cn.liweixi.dictionary.domain.DictionaryType;
import cn.liweixi.dictionary.query.DictionaryTypeQuery;
import cn.liweixi.dictionary.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "字典类型的API",description="字典相关的功能")
@RequestMapping("/  ")
@RestController
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    //查询所有
    @GetMapping
    public List<DictionaryType> findAll(){
        return dictionaryTypeService.findAll();
    }

    //根据id查询数据
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public DictionaryType findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return dictionaryTypeService.findById(id);
    }

    //修改或添加
    @PutMapping
    public Result addOrUpdate(@RequestBody DictionaryType dictionaryType) {
        try {
            if (null == dictionaryType.getId()){
                //添加数据
                dictionaryTypeService.add(dictionaryType);
            }else{
                //修改数据
                dictionaryTypeService.updateById(dictionaryType);
            }
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    //删除数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.delete(id);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

//    //分页查询
//    @PostMapping
//    public PageList<DictionaryType> queryByPage(@RequestBody DictionaryTypeQuery query){
//        return typeService.queryByPage(query);
//    }

    //批量删除
    @PatchMapping
    public Result batchDel(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.batchDel(ids);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }
}
